# -*- coding: utf-8 -*-
import os,re
import pymssql


serverName = '127.0.0.1'
#登陆用户名和密码
userName = 'sa'
passWord = '123456'
#建立连接并获取cursor
conn = pymssql.connect(serverName , userName , passWord, "snmplog")
cursor = conn.cursor()

for root, dirs, files in os.walk('d'):
        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list
        for fo in dirs:
            if 'hb' in fo:
                for r2, d2, f2 in os.walk(os.path.join(root, fo)):
                    for f22 in f2:
                        print('insert into ',fo,f22,re.split('-|\.',f22)[-2])
                        with open(os.path.join(root, fo, f22)) as f:
                            for k in f.readlines()[1:]:
                                d=k.split(',')
                                sql="insert into snmp(jifang,ip,unix,sentbytes) values('%s','%s','%s',%s)" % (fo,re.split('-|\.',f22)[-2],d[0].replace('_',' '),d[15])
                                cursor.execute(sql)
                        conn.commit()
        # for ff in files:
        #     print('insert into ',os.path.join(root, ff),re.split('-|\.',ff)[-2])
        #     with open(os.path.join(root, ff)) as f:
        #         for k in f.readlines()[1:]:
        #             d=k.split(',')
        #             sql="insert into snmp(jifang,ip,unix,sentbytes) values('%s','%s','%s',%s)" % (fo,re.split('-|\.',ff)[-2],d[0].replace('_',' '),d[15])
        #             cursor.execute(sql)
        #     conn.commit()
conn.close()